package com.mrwang.controller;

import com.mrwang.PageResult;
import com.mrwang.pojo.Brand;
import com.mrwang.service.BrandService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {
    @Resource
    BrandService brandService;
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> brandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc

    ){
        PageResult<Brand> result= brandService.brandByPage(key,page,rows,sortBy,desc);
        if (CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids")List<Long> cids){
        brandService.saveBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> brandByCid(@PathVariable("cid") Long cid){
        List<Brand> brandList= brandService.brandByCid(cid);
        if (CollectionUtils.isEmpty(brandList)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brandList);
    }

    @GetMapping("getBrandInfo/{brandId}")
    public ResponseEntity<Brand> getBrandInfo(@PathVariable("brandId") Long brandId){
        Brand brand= brandService.queryBrandInfoById(brandId);
        return ResponseEntity.ok(brand);
    }

}
